{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Backtrader量化平台教程-Portfolio级别的回测（九）\n",
    "\n",
    "使用的时候，笔者的函数只需要如下的设置：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = \"2017-04-01\"\n",
    "end_date = \"2017-06-20\"\n",
    "trading_csv_name = 'trading_data_two_year.csv'\n",
    "portfolio_csv_name = 'port_two_year.csv'\n",
    "benchmark_csv_name = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " 然后我们就可以回测了。笔者把回测的类封装了起来，只要调用笔者的回测类就可以了。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "begin = datetime.datetime.now()\n",
    "result_dict = bt_backtest.backtrader_backtest(start_date=start_date, end_date=end_date, trading_csv_name=trading_csv_name, \\\n",
    "                                              portfolio_csv_name=portfolio_csv_name, bechmark_csv_name=benchmark_csv_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后，回测结束后输出评价指标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-1-35a4f0cb707e>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-1-35a4f0cb707e>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    print \"time elapse:\", (end - begin)\u001b[0m\n\u001b[1;37m          ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "\n",
    "end = datetime.datetime.now()\n",
    "print \"time elapse:\", (end - begin)\n",
    "print 'Start Portfolio Value: %.2f' % result_dict['start_cash']\n",
    "print 'Final Portfolio Value: %.2f' % result_dict['final_value']\n",
    "print 'Total Return:', result_dict['total_return']\n",
    "print 'Sharpe Ratio :', result_dict['sharpe_ratio'] #* 2 # todo there should be consider!\n",
    "print 'Max Drowdown:', result_dict['max_drowdown'] * 2\n",
    "print 'Max Drowdown Money:', result_dict['max_drowdown_money']\n",
    "print \"Trade Information\", result_dict['trade_info']\n",
    " \n",
    "result = pd.read_csv('result.csv', index_col=0)\n",
    "result.plot()\n",
    "plt.show()\n",
    "position_info = pd.read_csv('position_info.csv', index_col=0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
